// ActionScript file
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;

import org.flexlancer.Logger.Logger;
import org.flexlancer.databases.Customers;
import org.flexlancer.events.TypeInSearchItemSelected;

[Bindable]
private var txt_input:String = "";
[Bindable]
private var dpCustomers:Array;

private var prev_list_index:Number = -1;
private var complete_list:Array;

private function OnCC():void{
	try{
		this.RefreshData();
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function RefreshData():void{
	try{
		this.complete_list = Customers.List();
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private var _disable_search:Boolean = false;

public function set DisableSearch(val:Boolean):void{
	try{
		this._disable_search = val;
		this.dgCustomers.visible = !val;
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function ShowFilterData():void{
	try{
		this.dpCustomers = new Array();
		
		var keyString:String = this.txtInput.text.toLowerCase();
		
		for each ( var singleCustomer:Object in this.complete_list ){
			var field1:String = singleCustomer.customername.toLowerCase();
			
			if ( field1.indexOf(keyString) > - 1 ){
				this.dpCustomers.push(singleCustomer);
			}
		}
		
		this.dgCustomers.dataProvider = this.dpCustomers;
	}catch(err:Error){
		Logger.LogE(err);
	}
}
private function OnInput_Change():void{
	try{
		if ( ! this._disable_search ){
			if ( this.txtInput.text.length == 0 ){
				this.dgCustomers.visible = false;
			}else{
				this.dgCustomers.visible = true;
			}
		}
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function OnInput_KeyUp(evt:KeyboardEvent):void{
	try{
		if ( evt.altKey ) return;
		switch(evt.keyCode){
			case Keyboard.ESCAPE:
				this.txtInput.text = "";
				this.dgCustomers.visible = false;
			break;
			case Keyboard.UP:
				if ( this.txtInput.text != ""){
					if ( this.dpCustomers.length > 0 ){
						this.dgCustomers.selectedIndex = this.dpCustomers.length - 1;
						this.dgCustomers.setFocus();
					}
				}
			break;
			case Keyboard.DOWN:
				if ( this.txtInput.text != "" ){
					if ( this.dpCustomers.length > 0 ){
						this.dgCustomers.selectedIndex = 0;
						this.dgCustomers.setFocus();
					}
				}
			break;
			default:
				this.ShowFilterData();
			break;
		}
	}catch(err:Error){
		Logger.LogE(err);
	}
}
private function OnDgCustomers_KeyUp(evt:KeyboardEvent):void{
	try{
		switch(evt.keyCode){
			case Keyboard.UP:
				if ( this.prev_list_index == this.dgCustomers.selectedIndex ){
					if ( this.dgCustomers.selectedIndex == 0 ) {
						this.txtInput.setFocus();
						this.dgCustomers.selectedIndex = -1;
						this.prev_list_index = -1
					}
				}
			break;
			case Keyboard.DOWN:
				if ( this.prev_list_index == this.dgCustomers.selectedIndex ) {
					if ( this.dgCustomers.selectedIndex == this.dpCustomers.length - 1 ) {
						this.txtInput.setFocus();
						this.dgCustomers.selectedIndex = -1;
						this.prev_list_index = -1;
					}
				}
			break;
			case Keyboard.ESCAPE:
				this.Reset();
			break;
			case Keyboard.ENTER:
				if ( this.dgCustomers.selectedIndex > -1 ){
					this.dispatchEvent(new TypeInSearchItemSelected(this.dgCustomers.selectedItem,TypeInSearchItemSelected.ITEM_SELECTED,true));
					this.txtInput.text = this.dgCustomers.selectedItem.customername;
					this.Reset();
				}
			break;
		}
		this.prev_list_index = this.dgCustomers.selectedIndex;
	}catch(err:Error){
		Logger.LogE(err);
	}
}
private function Reset():void{
	try{
		this.dgCustomers.visible = false;
		this.txtInput.setFocus();
		//this.txtInput.text = "";
		this.dgCustomers.selectedIndex = -1;
	}catch(err:Error){
		Logger.LogE(err);
	}
}